<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;
use Think\Page;
use Think\UploadFile;

/**
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class PoliceController extends AdminController {
    //单位表
    private $infoModel;
    //单位配置表
    private $infoConfModel;
    //省市区表
    private $districtModel;
    public function __construct(){
        parent::__construct();
        $this->infoModel = M('information');
        $this->infotypeModel = M('information_type');
        $this->infoModel->create();
        $this->infotypeModel->create();
    }
    public function infoList()
    {
        if($_SESSION['groupid']==2){
            $con['uid'] = $_SESSION['onethink_admin']['user_auth']['uid'];
        }
        if (I('keyword')) {
            $keyword               = I('keyword');
            $con['title|content'] = array('like', '%' . $keyword . '%');
            // $this->assign('keyword', $keyword);
        }
        if (I('area')) {
            $con['areaid'] = I('area');
        }
        if (I('start') && I('end')) {
            $con['ctime'] = array(between, array(strtotime(I('start')), strtotime(I('end'))));
        }
        $params = array(
            'keyword' => I('keyword'),
            'area'    => I('area'),
            'start'   => I('start'),
            'end'     => I('end'),
        );
        // dump($_POST);
        $Page     = new Page($count, 10, $params); // 实例化分页类 传入总记录数和每页显示的记录数
        $count = M('information_view')->where($con)->count();//查询总记录数
        $Page = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $infoList = M('information_view')->where($con)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select(); //$_POST 是查询条件
        $this->assign('list',$infoList);
        $areas = M('area')->where('father=370300')->select();
        $this->assign('areas', $areas);
        $this->assign('_page',$show);
        $this->display();
    }
    public function infoType()
    {
        if(IS_POST){
            $data['title'] = I(I('id'));
            $result = $this->infotypeModel->where('id = '.I('id'))->save($data);
            if($result){
                $this->success('更新成功');
            }
        }else{
            $tyepList = $this->infotypeModel->select();
            $this->assign('list',$tyepList);
            $this->display();
        }
    }
    public function updateInfo()
    {

        if(IS_POST){
            if(I('tupian2')){
                $this->infoModel->tupian = I('tupian2');
            }
            if($_FILES['tupian']['name']){
                $this->infoModel->tupian = I('tupian2');
                $upload = new UploadFile();// 实例化上传类
                $upload->maxSize  = 3145728 ;// 设置附件上传大小
                $upload->allowExts  = array('png','jpg','bmp','ico','jpeg','gif');// 设置附件上传类型
                $upload->savePath =  './Uploads/Picture/'.date('Ymd').'/';// 设置附件上传目录
                if(!$upload->upload()) {// 上传错误提示错误信息
                  $this->error($upload->getErrorMsg());
                }else{// 上传成功 获取上传文件信息
                  $info =  $upload->getUploadFileInfo();
                }
                $info[0]['savepath']=substr($info[0]['savepath'], 1); 
                $this->infoModel->tupian = $info[0]['savepath'].$info[0]['savename'];
            }
            $this->infoModel->uid = $_SESSION['onethink_admin']['user_auth']['uid'];
            $this->infoModel->areaid = $_SESSION['areaid'];
            $this->infoModel->content = I('content');
            $this->infoModel->status = 0;
            $this->infoModel->ctime = time();
            switch (I('a_type')) {
                case 'add':
                    $result = $this->infoModel->add();
                    break;
                case 'edit':
                    $result = $this->infoModel->where('id = '.I('id'))->save();
                    break;
            }
            if($result){
                $this->success('更新成功',U('infoList'));
            }else{
                $this->error('更新失败');
            }
        }else{

            $infotype = $this->infotypeModel->select();
            $this->assign('infotype',$infotype);
            $info = $this->infoModel->where('id = '.I('id'))->find();
            $this->assign('data',$info);
            $this->assign('a_type',I('a_type'));
            $this->display();
        }
    }
  /**

     * 通用数据添加函数

     * @param  string $module 数据库名称

     * @param  array  $data   字段

     * @param  string $url    成功跳转的链接

     * @return [type]         [description]

     */

    public function adddata($module = '', $datas = array(), $url = '')

    {

        if ($module) {

            $allocation_type = M($module);

            $data            = $allocation_type->create();

            if ($data) {

                foreach ($datas as $key => $value) {

                    if (array_key_exists($key, $data)) {

                        $data[$key] = $value;

                    } else {

                        $this->error('不包含字段名称！');

                    }

                }

                $data['status'] = 1;

                $data['ctime']  = NOW_TIME;

                // dump($data);exit();

                $id = $allocation_type->add($data);

                if ($id) {

                    $this->success('新增成功!', U($url));

                } else {

                    $this->error('新增失败!');

                }

            } else {

                $this->error('新增失败!');

            }

        }



    }



    /**

     * 通用数据修改函数

     * @param  string $module 数据库名称

     * @param  array  $data   字段

     * @param  string $url    成功跳转的链接

     * @return [type]         [description]

     */

    public function savedata($module = '', $datas = array(), $url = '')

    {

        if ($module) {

            $allocation_type = M($module);

            $data            = $allocation_type->create();

            if ($data) {

                foreach ($datas as $key => $value) {

                    if (array_key_exists($key, $data)) {

                        $data[$key] = $value;

                    } else {

                        $this->error('不包含字段名称！');

                    }

                }

                $data['utime'] = NOW_TIME;

                // dump($data);exit();

                $id = $allocation_type->save($data);

                if ($id) {
                    $this->success('更新成功!', U($url));
                } else {
                    $this->error('更新失败!');
                }
            } else {
                $this->error('更新失败!');
            }
        }
    }
    /**
     * 删除后台菜单
     * @author yangweijie <yangweijiester@gmail.com>
     */
    public function del(){
        if(M('information')->where('id = '.I('id'))->delete()){
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }
    public function changeState()
    {
        $data['status'] = I('status');
        if(I('back')){
            $data['back'] = I('back');
        }
        $result = M('information')->where('id = '.I('id'))->save($data);
        if($result){
            $this->success('更新成功',U("infoList"));
        }else{
            $this->error('更新失败');
        }

    }
}
